package com.gjzhao.fmmall.controller;

import com.gjzhao.fmmall.service.BrandService;
import com.gjzhao.fmmall.vo.BrandVO;
import com.gjzhao.fmmall.vo.RespCode;
import com.gjzhao.fmmall.vo.RespResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 品牌管理 Controller
 *  作用：validator相关注解配合aop实现参数校验
 *  - 1.@Validated 校验注解
 *  - 2.BindingResult 注入对象，获取校验结果[该校验方式的缺点]
 * @Author: gjzhao
 * @Date Created in 2022-01-24 10:24 上午
 */
@RestController
@RequestMapping(value = "/brand")
@Api(value = "品牌数据管理", tags = "品牌管理")
public class BrandController extends BaseController {

    @Autowired
    private BrandService brandService;

    @PostMapping(value = "/add")
    @ApiOperation(value = "添加品牌(测试 @Validated 参数校验功能)")
    public RespResultVO add(@Validated @RequestBody BrandVO brand, BindingResult result) {

        return  brandService.addByValidated(brand);
    }

    @PostMapping(value = "/addByGlobalException")
    @ApiOperation(value = "添加品牌(测试 ApiException 全局异常功能)")
    public RespResultVO addByGlobalException(@RequestBody BrandVO brand) {

        brandService.addByGlobalException(brand);

        return RespResultVO.success(brand);
    }
}
